package com.jikexueyuan.drawingboard;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.graphics.PorterDuffXfermode;
import android.graphics.PointF;
import com.jikexueyuan.drawingboard.PenTrackReplay;

public class MyView extends SurfaceView implements Callback,OnTouchListener{

	private Paint mPaint = new Paint();
    private Paint mEraserPaint = new Paint();
	private Path path = new Path();
    private Path mEraserPath = new Path();
    private PenTrackReplay mPenTrackReplay = new PenTrackReplay(this);
    private int count_down = 0;
    private int count_move = 0;
    private int count_up = 0;
    public boolean bSelectEraser = false;
    private int currentColor = Color.RED;

    // 和录屏相关的变量
    private int count_point = 0;
    private PointF mPoint = new PointF();
    private boolean bRecordEnable = false;
	
	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		getHolder().addCallback(this);
        setPaintStyle(currentColor);
        setEraserPaintStyle();
        //mPaint.setColor(currentColor);
        //mPaint.setTextSize(10);
        //mPaint.setAntiAlias(true);
        //mPaint.setStyle(Style.STROKE);
		setOnTouchListener(this);
        //mPaint.setStrokeWidth(6.0f);

	}

	private void draw(){
		Canvas canvas = getHolder().lockCanvas();
		canvas.drawColor(Color.WHITE);
        canvas.drawPath(path, mPaint);
		getHolder().unlockCanvasAndPost(canvas);
	}
	
	public void clear(){
		path.reset();
        mEraserPath.reset();
		draw();
	}

    // 和录屏相关的函数，清除保存点
    public void resetRecordPoints() {
        count_point = 0;
        mPenTrackReplay.clearBuffer();
    }

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		draw();
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
        int eventAction = event.getAction();
        long time = System.currentTimeMillis();

        // 和录屏相关的逻辑，在回放过程中禁止输入
        if(mPenTrackReplay.getReplayEnable()) {
            return false;
        }
        mPoint.set(event.getX(), event.getY());

		switch (eventAction) {
		case MotionEvent.ACTION_DOWN:
            path.moveTo(mPoint.x, mPoint.y);
            System.out.println("onTouch: ACTION_DOWN: count_down=" + count_down + ", x=" + event.getX() + ", y=" + event.getY());
            count_down++;
            draw();
			break;

		case MotionEvent.ACTION_MOVE:
            path.lineTo(mPoint.x, mPoint.y);
            System.out.println("onTouch: ACTION_MOVE: count_move=" + count_move + ", x=" + event.getX() + ", y=" + event.getY() );
            count_move++;
			draw();
			break;

        case MotionEvent.ACTION_UP:
            path.lineTo(mPoint.x, mPoint.y);
            System.out.println("onTouch: ACTION_UP: count_up=" + count_up + ", x=" + event.getX() + ", y=" + event.getY() );
            count_up++;
            draw();
            break;
        default:
            System.out.println("onTouch: [Warning] should not go to default switch.");
            break;
		}

        // 和录屏相关的调用，记录笔迹点属性、时间戳以及点数
        if(bRecordEnable) {
            PenTrackContainer penTrack = new PenTrackContainer();
            penTrack.setPenTrackPoint(Color.RED, 6, eventAction, mPoint, time);
            mPenTrackReplay.record(penTrack, count_point);
            //mPenTrackReplay.mPenTrack[count_point].SetPenTrackPoint(Color.RED, 6, eventAction, mPoint, time);
            count_point++;
        }
		return true;
	}

    private void setEraserPaintStyle(){
        int eraserSize = 20;
        mEraserPaint.setAntiAlias(true);
        mEraserPaint.setDither(true);
        mEraserPaint.setColor(Color.TRANSPARENT);
        mEraserPaint.setStrokeWidth(eraserSize);
        mEraserPaint.setStyle(Paint.Style.STROKE);
        mEraserPaint.setStrokeJoin(Paint.Join.ROUND);
        mEraserPaint.setStrokeCap(Paint.Cap.SQUARE);
        mEraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
    }

    //设置画笔样式
    public void setPaintStyle(int color){
        //mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        if(color == Color.WHITE) {
            mPaint.setStrokeWidth(20);
        }
        else {
            mPaint.setStrokeWidth(6.0f);
        }
        mPaint.setColor(color);
    }

    //设置画笔样式, 1-red pen, 0-erase
    public void selectPaintStyle(int color){

        if(color == 1){
            bSelectEraser = false;
        }
        //当选择的是橡皮擦时，设置颜色为白色
        if(color == 0){
            bSelectEraser = true;
        }
    }

    // 和录屏相关的函数
    public void replayPenTrack(VoiceRecorder voiceRecord) {
        mPenTrackReplay.replay(voiceRecord);
    }

    // 和录屏相关的函数
    public void setRecordEnable() {
        bRecordEnable = true;
    }

    // 和录屏相关的函数
    public void setRecordDisable() {
        bRecordEnable = false;
    }

    // 和录屏相关的函数
    public void saveStartTime() {
        long time = System.currentTimeMillis();
        PointF tmpPoint = new PointF();
        tmpPoint.set(0,0);
        if(bRecordEnable) {
            PenTrackContainer penTrack = new PenTrackContainer();
            penTrack.setPenTrackPoint(Color.RED, 6, MotionEvent.ACTION_CANCEL, mPoint, time);
            mPenTrackReplay.record(penTrack, count_point);

            //mPenTrackReplay.mPenTrack[count_point].SetPenTrackPoint(Color.RED, 6, MotionEvent.ACTION_CANCEL, tmpPoint, time);
            count_point++;
        }
    }

    // 和录屏回放相关的函数
    public void redrawPenTrack(int action, PointF point) {
        System.out.println("redrawPenTrack: action=" + action +", point.x=" + point.x + ", point.y=" + point.y);

        switch(action) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(point.x, point.y);
                draw();
                break;
            case MotionEvent.ACTION_MOVE:
                path.lineTo(point.x, point.y);
                draw();
                break;
            case MotionEvent.ACTION_UP:
                path.lineTo(point.x, point.y);
                draw();
                break;
            default:
                System.out.println("redrawPenTrack: [Warning] should not go to default switch.");
        }
    }

}
